#include "i80_lcd_panels.h"

#include <stddef.h>

const lcd_panel_reg_t panel_st7796s_w350ce024a_40z_reg_table[] = {
    {0x11, NULL, 0},                                                                                             //退出休眠模式
    {0x21, NULL, 0},                                                                                             //开启反色显示
    {0xF0, (uint8_t[]){0xC3}, 1},                                                                                //使能指令集2第1部分
    {0xF0, (uint8_t[]){0x96}, 1},                                                                                //使能指令集2第2部分
    {0x36, (uint8_t[]){0x48}, 1},                                                                                //显存访问方式: MY=0, MX=1, MV=0, ML=0, RGB=1, MH=0
    {0x3A, (uint8_t[]){0x55}, 1},                                                                                //像素颜色格式: RGB模式16位, MCU模式16位
    {0xB4, (uint8_t[]){0x01}, 1},                                                                                //显示反转控制
    {0xB7, (uint8_t[]){0xC6}, 1},                                                                                // Entry Mode设置
    {0xC1, (uint8_t[]){0x06}, 1},                                                                                // GVDD&GVCL设置
    {0xC2, (uint8_t[]){0xA7}, 1},                                                                                //源极&gamma驱动电流设置
    {0xC5, (uint8_t[]){0x18}, 1},                                                                                // VCOM设置
    {0xE0, (uint8_t[]){0xF0, 0x09, 0x0B, 0x06, 0x04, 0x15, 0x2F, 0x54, 0x42, 0x3C, 0x17, 0x14, 0x18, 0x1B}, 14}, //正gamma控制
    {0xE1, (uint8_t[]){0xF0, 0x09, 0x0B, 0x06, 0x04, 0x03, 0x2D, 0x43, 0x42, 0x3B, 0x16, 0x14, 0x17, 0x1B}, 14}, //负gamma控制
    {0xE8, (uint8_t[]){0x40, 0x8A, 0x00, 0x00, 0x29, 0x19, 0xA5, 0x33}, 8},                                      //显示参数配置
    {0xF0, (uint8_t[]){0x3C}, 1},                                                                                //失能指令集2第1部分
    {0xF0, (uint8_t[]){0x69}, 1},                                                                                //失能指令集2第2部分

    {0, NULL, 0xFF} //寄存器列表结束
};

const lcd_panel_reg_t panel_st7796s_cl35bc1017_40a_reg_table[] = {
    {0x11, NULL, 0},                                                                                             //退出休眠模式
    {0x20, NULL, 0},                                                                                             //关闭反色显示
    {0xF0, (uint8_t[]){0xC3}, 1},                                                                                //使能指令集2第1部分
    {0xF0, (uint8_t[]){0x96}, 1},                                                                                //使能指令集2第2部分
    {0x36, (uint8_t[]){0xE8}, 1},                                                                                //显存访问方式: MY=1, MX=0, MV=1, ML=0, RGB=1, MH=0 D1=0 D0=0/*可以改这里扫描方向，横屏竖屏*/
    {0x3A, (uint8_t[]){0x55}, 1},                                                                                //像素颜色格式: RGB模式16位, MCU模式16位
    {0xC2, (uint8_t[]){0xA7}, 1},                                                                                //源极&gamma驱动电流
    {0xC5, (uint8_t[]){0x27}, 1},                                                                                // VCOM
    {0xE0, (uint8_t[]){0xF0, 0x01, 0x06, 0x0F, 0x12, 0x1D, 0x36, 0x54, 0x44, 0x0C, 0x18, 0x16, 0x13, 0x15}, 14}, //正gamma控制
    {0xE1, (uint8_t[]){0xF0, 0x01, 0x05, 0x0A, 0x0B, 0x07, 0x32, 0x44, 0x44, 0x0C, 0x18, 0x17, 0x13, 0x16}, 14}, //负gamma控制
    {0xE8, (uint8_t[]){0x40, 0x82, 0x07, 0x18, 0x27, 0x0A, 0xB6, 0x33}, 8},                                      //显示参数配置
    {0xF0, (uint8_t[]){0x3C}, 1},                                                                                //失能指令集2第1部分
    {0xF0, (uint8_t[]){0x69}, 1},                                                                                //失能指令集2第2部分
    {0, NULL, 0xFF}                                                                                              //寄存器列表结束
};

const lcd_panel_reg_t panel_ili9488_cl35bc106_40a_reg_table[] = {
    {0x11, NULL, 0},
    {0xF7, (uint8_t[]){0xA9, 0x51, 0x2C, 0x82}, 4},
    {0xEC, (uint8_t[]){0x00, 0x02, 0x03, 0x7A}, 4},
    {0xC0, (uint8_t[]){0x13, 0x13}, 2},
    {0xC1, (uint8_t[]){0x41}, 1},
    {0xC5, (uint8_t[]){0x00, 0x28, 0x80}, 3},
    {0xB1, (uint8_t[]){0xB0, 0x11}, 2},
    {0xB4, (uint8_t[]){0x02}, 1},
    {0xB6, (uint8_t[]){0x02, 0x22}, 2},
    {0xB7, (uint8_t[]){0xC6}, 1},
    {0xBE, (uint8_t[]){0x00, 0x04}, 2},
    {0xE9, (uint8_t[]){0x00}, 1},
    {0xF4, (uint8_t[]){0x00, 0x00, 0x0F}, 3},
    {0xE0, (uint8_t[]){0x00, 0x04, 0x0E, 0x08, 0x17, 0x0A, 0x40, 0x79, 0x4D, 0x07, 0x0E, 0x0A, 0x1A, 0x1D, 0x0F}, 16},
    {0xE1, (uint8_t[]){0x00, 0x1B, 0x1F, 0x02, 0x10, 0x05, 0x32, 0x34, 0x43, 0x02, 0x0A, 0x09, 0x33, 0x37, 0x0F}, 16},
    {0xF4, (uint8_t[]){0x00, 0x00, 0x0F}, 3},
    {0x36, (uint8_t[]){0x3F}, 1},
    {0x3A, (uint8_t[]){0x55}, 1},
    {0, NULL, 0xFF}};

const lcd_panel_reg_t panel_ili9488_P0395H01_reg_table[] = {
    {0x11, NULL, 0}, // out sleep
    //{0x29, NULL, 0},
    //{0x21, NULL, 0},              //开启反色显示
    //{0x20, NULL, 0},              //关闭反色显示

    // {0x36, (uint8_t[]){0x68}, 1}, //显存访问方式: MY=0, MX=1, MV=1, ML=0, RGB=1, MH=0  D1=0  D0=0 横屏1
    {0x36, (uint8_t[]){0xB8}, 1}, //显存访问方式: MY=1, MX=0, MV=1, ML=1, RGB=1, MH=0  D1=0  D0=0 横屏2
    // {0x36, (uint8_t[]){0x18}, 1}, //显存访问方式: MY=0, MX=0, MV=0, ML=1, RGB=1, MH=0  D1=0  D0=0 竖屏1
    // {0x36, (uint8_t[]){0x08}, 1}, //显存访问方式: MY=0, MX=0, MV=0, ML=1, RGB=1, MH=0  D1=0  D0=0 竖屏2

    // MX MY 对应屏幕竖着看
    {0x3A, (uint8_t[]){0x55}, 1}, //像素颜色格式: 0x55RGB模式16位  0x66RGB模式18位
    {0xF7, (uint8_t[]){0xA9, 0x51, 0x2C, 0x02}, 4},
    {0XEC, (uint8_t[]){0x00, 0x02, 0x03, 0x7A}, 4},
    {0xC0, (uint8_t[]){0x13, 0x13}, 2},
    {0xC1, (uint8_t[]){0x41}, 1},
    {0xC5, (uint8_t[]){0x00, 0x28, 0x80, 0x28}, 4},
    {0xB1, (uint8_t[]){0xB0, 0x11}, 2},

    {0xB4, (uint8_t[]){0x02}, 1},
    {0xB6, (uint8_t[]){0x02, 0x22}, 2},
    {0xB7, (uint8_t[]){0xc6}, 1},
    {0xBE, (uint8_t[]){0x00, 0x04}, 2},
    {0xE9, (uint8_t[]){0x00}, 1},
    {0xF4, (uint8_t[]){0x00, 0x00, 0x0f}, 3},
    {0xE0, (uint8_t[]){0x00, 0x04, 0x0E, 0x08, 0x17, 0x0A, 0x40, 0x79, 0x4D, 0x07, 0x0E, 0x0A, 0x1A, 0x1D, 0x0F}, 16},
    {0xE1, (uint8_t[]){0x00, 0x1B, 0x1F, 0x02, 0x10, 0x05, 0x32, 0x34, 0x43, 0x02, 0x0A, 0x09, 0x33, 0x37, 0x0F}, 16},
    {0xF4, (uint8_t[]){0x00, 0x00, 0x0f}, 3}, //解决电荷残留问题
    {0, NULL, 0xFF}};

const lcd_panel_reg_t panel_GC9A01_reg_table[] = {
    //{0x11, NULL, 0}, //out sleep

    {0xEF, NULL, 0},
    {0xEB, (uint8_t[]){0x14}, 1},

    {0xFE, NULL, 0},
    {0xEF, NULL, 0},
    {0x36, (uint8_t[]){0x08}, 1}, //显存访问方式: MY=0, MX=0, MV=0, ML=1, RGB=1, MH=0  D1=0  D0=0 此处RGB屏必要的，否则颜色不正常，需使能RGB
    {0xEB, (uint8_t[]){0x14}, 1},
    {0x84, (uint8_t[]){0x40}, 1},
    {0x85, (uint8_t[]){0xFF}, 1},
    {0x86, (uint8_t[]){0xFF}, 1},
    {0x87, (uint8_t[]){0xFF}, 1},
    {0x88, (uint8_t[]){0x0A}, 1},
    {0x89, (uint8_t[]){0x21}, 1},
    {0x8A, (uint8_t[]){0x00}, 1},
    {0x8B, (uint8_t[]){0x80}, 1},
    {0x8C, (uint8_t[]){0x01}, 1},
    {0x8D, (uint8_t[]){0x01}, 1},
    {0x8E, (uint8_t[]){0xFF}, 1},
    {0x8F, (uint8_t[]){0xFF}, 1},
    {0xB6, (uint8_t[]){0x00, 0x20}, 2},
    // call orientation
    {0x3A, (uint8_t[]){0x05}, 1},
    {0x90, (uint8_t[]){0x08, 0x08, 0X08, 0X08}, 4},
    {0xBD, (uint8_t[]){0x06}, 1},
    {0xBC, (uint8_t[]){0x00}, 1},
    {0xFF, (uint8_t[]){0x60, 0x01, 0x04}, 3},
    {0xC3, (uint8_t[]){0x13}, 1},
    {0xC4, (uint8_t[]){0x13}, 1},
    {0xC9, (uint8_t[]){0x22}, 1},
    {0xBE, (uint8_t[]){0x11}, 1},
    {0xE1, (uint8_t[]){0x10, 0x0E}, 2},
    {0xDF, (uint8_t[]){0x21, 0x0C, 0x02}, 3},
    {0xF0, (uint8_t[]){0x45, 0x09, 0x08, 0x08, 0x26, 0x2A}, 6},
    {0xF1, (uint8_t[]){0x43, 0x70, 0x72, 0x36, 0x37, 0x6F}, 6},
    {0xF2, (uint8_t[]){0x45, 0x09, 0x08, 0x08, 0x26, 0x2A}, 6},
    {0xF3, (uint8_t[]){0x43, 0x70, 0x72, 0x36, 0x37, 0x6F}, 6},
    {0xED, (uint8_t[]){0x1B, 0x0B}, 2},
    {0xAE, (uint8_t[]){0x77}, 1},
    {0xCD, (uint8_t[]){0x63}, 1},
    {0x70, (uint8_t[]){0x07, 0x07, 0x04, 0x0E, 0x0F, 0x09, 0x07, 0X08, 0x03}, 9},
    {0xE8, (uint8_t[]){0x34}, 1},
    {0x62, (uint8_t[]){0x18, 0x0D, 0x71, 0xED, 0x70, 0x70, 0x18, 0X0F, 0x71, 0xEF, 0x70, 0x70}, 12},
    {0x63, (uint8_t[]){0x18, 0x11, 0x71, 0xF1, 0x70, 0x70, 0x18, 0X13, 0x71, 0xF3, 0x70, 0x70}, 12},
    {0x64, (uint8_t[]){0x28, 0x29, 0xF1, 0x01, 0xF1, 0x00, 0x07}, 7},
    {0x66, (uint8_t[]){0x3C, 0x00, 0xCD, 0x67, 0x45, 0x45, 0x10, 0X00, 0x00, 0x00}, 10},
    {0x67, (uint8_t[]){0x00, 0x3C, 0x00, 0x00, 0x00, 0x01, 0x54, 0X10, 0x32, 0x98}, 10},
    {0x74, (uint8_t[]){0x10, 0x85, 0x80, 0x00, 0x00, 0x4E, 0x00}, 7},
    {0x98, (uint8_t[]){0x3E, 0x07}, 2},
    {0x35, NULL, 0},
    {0x21, NULL, 0},
    {0x11, NULL, 0}, // 0x80 delay flag
    {0x29, NULL, 0}, // 0x80 delay flag
    {0, NULL, 0xFF},
};

const lcd_panel_reg_t panel_NT35510_reg_table[] = {
    {0xF000, (uint8_t[]){0x55, 0xAA, 0x52, 0x08, 0x01}, 5},
    {0xB600, (uint8_t[]){0x34, 0x34, 0x34}, 3},
    {0xB000, (uint8_t[]){0x0D, 0x0D, 0x0D}, 3},
    {0xB700, (uint8_t[]){0x34, 0x34, 0x34}, 3},
    {0xB100, (uint8_t[]){0x0D, 0x0D, 0x0D}, 3},
    {0xB800, (uint8_t[]){0x24, 0x24, 0x24}, 3},
    {0xB900, (uint8_t[]){0x34, 0x34, 0x34}, 3},
    {0xB300, (uint8_t[]){0x0F, 0x0F, 0x0F}, 3},
    {0xBA00, (uint8_t[]){0x24, 0x24, 0x24}, 3},
    {0xB500, (uint8_t[]){0x08, 0x08}, 2},
    {0xBC00, (uint8_t[]){0x00, 0x78, 0x00}, 3},
    {0xBD00, (uint8_t[]){0x00, 0x78, 0x00}, 3},
    {0xBE00, (uint8_t[]){0x00, 0x89}, 2},

    {0xD100, (uint8_t[]){0x00, 0x37, 0x00, 0x53, 0x00, 0x79, 0x00, 0x97, 0x00, 0xB1, 0x00, 0xD5, 0x00, 0xF4, 0x01, 0x23, 0x01, 0x49, 0x01, 0x87, 0x01, 0xB6, 0x02, 0x00, 0x02, 0x3B, 0x02, 0x3D, 0x02, 0x75, 0x02, 0xB1, 0x02, 0xD5, 0x03, 0x09, 0x03, 0x28, 0x03, 0x52, 0x03, 0x6B, 0x03, 0x8D, 0x03, 0xA2, 0x03, 0xBB, 0x03, 0xC1, 0x03, 0xC1}, 52},

    {0xD200, (uint8_t[]){0x00, 0x37, 0x00, 0x53, 0x00, 0x79, 0x00, 0x97, 0x00, 0xB1, 0x00, 0xD5, 0x00, 0xF4, 0x01, 0x23, 0x01, 0x49, 0x01, 0x87, 0x01, 0xB6, 0x02, 0x00, 0x02, 0x3B, 0x02, 0x3D, 0x02, 0x75, 0x02, 0xB1, 0x02, 0xD5, 0x03, 0x09, 0x03, 0x28, 0x03, 0x52, 0x03, 0x6B, 0x03, 0x8D, 0x03, 0xA2, 0x03, 0xBB, 0x03, 0xC1, 0x03, 0xC1}, 52},

    {0xD300, (uint8_t[]){0x00, 0x37, 0x00, 0x53, 0x00, 0x79, 0x00, 0x97, 0x00, 0xB1, 0x00, 0xD5, 0x00, 0xF4, 0x01, 0x23, 0x01, 0x49, 0x01, 0x87, 0x01, 0xB6, 0x02, 0x00, 0x02, 0x3B, 0x02, 0x3D, 0x02, 0x75, 0x02, 0xB1, 0x02, 0xD5, 0x03, 0x09, 0x03, 0x28, 0x03, 0x52, 0x03, 0x6B, 0x03, 0x8D, 0x03, 0xA2, 0x03, 0xBB, 0x03, 0xC1, 0x03, 0xC1}, 52},

    {0xD400, (uint8_t[]){0x00, 0x37, 0x00, 0x53, 0x00, 0x79, 0x00, 0x97, 0x00, 0xB1, 0x00, 0xD5, 0x00, 0xF4, 0x01, 0x23, 0x01, 0x49, 0x01, 0x87, 0x01, 0xB6, 0x02, 0x00, 0x02, 0x3B, 0x02, 0x3D, 0x02, 0x75, 0x02, 0xB1, 0x02, 0xD5, 0x03, 0x09, 0x03, 0x28, 0x03, 0x52, 0x03, 0x6B, 0x03, 0x8D, 0x03, 0xA2, 0x03, 0xBB, 0x03, 0xC1, 0x03, 0xC1}, 52},

    {0xD500, (uint8_t[]){0x00, 0x37, 0x00, 0x53, 0x00, 0x79, 0x00, 0x97, 0x00, 0xB1, 0x00, 0xD5, 0x00, 0xF4, 0x01, 0x23, 0x01, 0x49, 0x01, 0x87, 0x01, 0xB6, 0x02, 0x00, 0x02, 0x3B, 0x02, 0x3D, 0x02, 0x75, 0x02, 0xB1, 0x02, 0xD5, 0x03, 0x09, 0x03, 0x28, 0x03, 0x52, 0x03, 0x6B, 0x03, 0x8D, 0x03, 0xA2, 0x03, 0xBB, 0x03, 0xC1, 0x03, 0xC1}, 52},

    {0xD600, (uint8_t[]){0x00, 0x37, 0x00, 0x53, 0x00, 0x79, 0x00, 0x97, 0x00, 0xB1, 0x00, 0xD5, 0x00, 0xF4, 0x01, 0x23, 0x01, 0x49, 0x01, 0x87, 0x01, 0xB6, 0x02, 0x00, 0x02, 0x3B, 0x02, 0x3D, 0x02, 0x75, 0x02, 0xB1, 0x02, 0xD5, 0x03, 0x09, 0x03, 0x28, 0x03, 0x52, 0x03, 0x6B, 0x03, 0x8D, 0x03, 0xA2, 0x03, 0xBB, 0x03, 0xC1, 0x03, 0xC1}, 52},

    {0xF000, (uint8_t[]){0x55, 0xAA, 0x52, 0x08, 0x00}, 5},
    {0xB000, (uint8_t[]){0x08, 0x05, 0x02, 0x05, 0x02}, 5},


    {0xB600, (uint8_t[]){0x08}, 1},
    {0xB500, (uint8_t[]){0x50}, 1},
//   _bus->writeCommand16(0xB600);
//   _bus->write16(0x08);
//   _bus->writeCommand16(0xB500);
//   _bus->write16(0x50); //## SDT: //0x6b ?? 480x854  0x50 ?? 480x800

    {0xB700, (uint8_t[]){0x00, 0x00}, 2},
    {0xB800, (uint8_t[]){0x01, 0x05, 0x05, 0x05}, 4},
    {0xBC00, (uint8_t[]){0x00, 0x00, 0x00}, 3},
    {0xCC00, (uint8_t[]){0x03, 0x00, 0x00}, 3},
    {0xBD00, (uint8_t[]){0x01, 0x84, 0x07, 0x31, 0x00, 0x01}, 6},
    {0xFF00, (uint8_t[]){0xAA, 0x55, 0x25, 0x01}, 4},


    {0xB500, (uint8_t[]){0x00}, 1},
    {0xB500, (uint8_t[]){0x55}, 1},
//   _bus->writeCommand16(NT35510_TEON);
//   _bus->write16(0x00);
//   _bus->writeCommand16(NT35510_COLMOD);
//   _bus->write16(0x55); //0x55=16bit Mode
//   _bus->writeCommand16(NT35510_SLPOUT);
//   _bus->endWrite();


};
